<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>添加菜单</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
	<div class="layui-form-item">
		<input type="hidden" name="menuId" value="${sysMenuEntity.menuId}"/>
        <label class="layui-form-label">一级目录</label>
        <div class="layui-input-block">
            <select name="firstMenu" lay-filter="firstMenu" id="firstMenu">                     
            </select>
            <tip>添加一级目录可不选</tip>
        </div>        
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">二级目录</label>
        <div class="layui-input-block">
            <select name="secondMenu" lay-filter="secondMenu" id="secondMenu">                     
            </select>
            <tip>添加一，二级目录可不选</tip>
        </div>
        <input type="hidden" name="parentId" value="${sysMenuEntity.parentId}" id="parentId"/>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label required">菜单名称</label>
        <div class="layui-input-block">
            <input type="text" name="menuName" lay-verify="required" lay-reqtext="菜单名称不能为空" placeholder="请输入菜单名称" value="${sysMenuEntity.menuName}" class="layui-input">            
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">菜单链接</label>
        <div class="layui-input-block">
            <input type="text" name="menuUrl" placeholder="请输入菜单链接" value="${sysMenuEntity.menuUrl?default('')}" class="layui-input">            
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">权限标识</label>
        <div class="layui-input-block">
            <input type="text" name="permission" placeholder="请输入权限标识" value="${sysMenuEntity.permission?default('')}" class="layui-input">            
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">排序号</label>
        <div class="layui-input-block">
            <input type="number" name="sort" placeholder="请输入排序号" value=${sysMenuEntity.sort} class="layui-input">            
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">菜单图标</label>
        <div class="layui-input-block">
            <input type="text" name="menuPicLeave" placeholder="请输入菜单图标" value="${sysMenuEntity.menuPicLeave}" class="layui-input">            
        </div>
    </div>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            <button class="layui-btn layui-btn-normal" id="cancel">取消</button>
        </div>
    </div>
</div>
<script src="../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form'], function () {
        var form = layui.form,
            layer = layui.layer,
            layarea = layui.layarea,
            $ = layui.$;
       //加载下拉列表
       layer.ready(function(){
    	   if($("#parentId").val()==0){
    		  var info ="<option value=-1>请选择</option>";     	 
	    	   $.post("../sysMenu/roleMenuList",{'parentId':0}, function (data) {    		    
	    		    for (var i = 0; i < data.length; i++) {    
	    		        info += "<option value=" + data[i].menuId + ">" + data[i].menuName + "</option>";        
	    		    }
	    		    $("#firstMenu").append(info);
	    		    form.render('select');
	    		 });
    	   	}else{
    	   		$.post("../sysMenu/parentIdList",{'parentId':$("#parentId").val()}, function (data) {
    	   			var firstMenu= data.firstMenu;
    	   			var info ="<option value=-1>请选择</option>";
	    		    for (var i = 0; i < firstMenu.length; i++) {
	    		    	if(data.firstId == firstMenu[i].menuId){
	    		    		info += "<option value=" + firstMenu[i].menuId + " selected='selected'>" + firstMenu[i].menuName + "</option>";
	    		    	}else{
	    		    		info += "<option value=" + firstMenu[i].menuId + ">" + firstMenu[i].menuName + "</option>";
	    		    	}	    		    		    		                
	    		    }
	    		    $("#firstMenu").append(info);	    		    
	    		    var info2 ="<option value=-1>请选择</option>";
	    		    var secondMenu= data.secondMenu;
	    		    if(data.secondId==-1){
	    		    	 for (var i = 0; i < secondMenu.length; i++) {    
	 	    		        info2 += "<option value=" + secondMenu[i].menuId + ">" + secondMenu[i].menuName + "</option>";        
	 	    		    }	 	    		    
	    		    }else{
	    		    	for (var i = 0; i < secondMenu.length; i++) {
		    		    	if(data.secondId == secondMenu[i].menuId){
		    		    		info2 += "<option value=" + secondMenu[i].menuId + " selected='selected'>" + secondMenu[i].menuName + "</option>";
		    		    	}else{
		    		    		info2 += "<option value=" + secondMenu[i].menuId + ">" + secondMenu[i].menuName + "</option>";
		    		    	}
	    		    	}
	    		    }
	    		    $("#secondMenu").append(info2);
	    		    form.render('select');
	    		 });
    	   	}			    	  
        }); 
        var iframeIndex = parent.layer.getFrameIndex(window.name);
        //监听一级目录二级目录取值
        form.on('select(firstMenu)', function(data){
        	  var firstId=data.value;
        	  $.post("../sysMenu/roleMenuList",{'parentId':firstId}, function (data) {
      		    var info = "<option value=-1>请选择</option>";
      		    for (var i = 0; i < data.length; i++) {    
      		        info += "<option value=" + data[i].menuId + ">" + data[i].menuName + "</option>";        
      		    }
      		    $("#secondMenu").append(info);
      		    form.render('select');
      		 });        	         	
        });
        //监听提交
        form.on('submit(saveBtn)', function (data) {
        	if($("#firstMenu").val()=="${sysMenuEntity.menuId}"&&$("#secondMenu").val()==-1){
        		layer.msg("一级目录不能为其本身");
        		return flase;
        	}
        	if($("#secondMenu").val()=="${sysMenuEntity.menuId}"){
        		layer.msg("二级目录不能为其本身");
        		return flase;
        	}
        	$.ajax({
                type : "POST", //提交方式
                url : "../sysMenu/update",//路径
                contentType: "application/json;charset=utf-8",
                data : JSON.stringify(data.field),//数据，这里使用的是Json格式进行传输
                dataType: "json",
                success : function(result) {//返回数据根据结果进行相应的处理
                    if (result.code == "0") {
                        var index = layer.msg("成功", {
                            time: 300 //如果不配置，默认是3秒）
                        },function () {
                        	// 关闭弹出层
                            layer.close(index);
                            parent.layer.close(iframeIndex);
                        })
                    }else{
                        layer.msg(result.msg);
                    }
                }
            });
            return false;
        });
        
        $('#cancel').on("click", function () {
             parent.layer.close(iframeIndex);
        });
    });
</script>
</body>
</html>